import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

// 获取当前时间戳
const timeStamp = new Date().getTime();

// https://vite.dev/config/
export default defineConfig({
  plugins: [vue(),
  AutoImport({
    imports: ['vue']
  }),
  Components({
    resolvers: []
  })
  ],
  //端口号
  server: {
    port: 8001,
    host: '0.0.0.0',
    proxy: { //代理配置，解决跨域
      '/api': {
         // 当前环境为线上时，自动使用production环境变量里的VITE_API_BASEURL线上接口地址
        target: "http://127.0.0.1:8888",
        changeOrigin: true,   //修改源
        rewrite: (path) => path.replace(/^\/api/, '') //api替换为''
      },
    }
  },
  //打包配置
  build: {
    // 打包文件夹名称
    outDir: 'dist',
    // 打包后去掉console语句
    terserOptions: {
      compress: {
        drop_console: true
      }
    },
    compress: {
      drop_console: true,
      drop_debugger: true
    },
    // 打包后的文件名
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${timeStamp}.js`,
        chunkFileNames: `assets/[name].${timeStamp}.js`,
        assetFileNames: `assets/[name].${timeStamp}.[ext]`
      }
    }
  }
})
